package com.rtx.wcs.std.controller;

import java.util.Map;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.rtx.wcs.std.bean.dto.RtxWcsResponse;
import com.rtx.wcs.std.handle.ristowcs.TaskDownHandleService;
import com.rtx.wcs.std.handle.wcstoris.BzTaskCallBackHandle;
import com.rtx.wcs.std.handle.wcstoris.TaskCallBackHandle;

import cn.hutool.core.lang.UUID;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = {"WCS任务"})
@RestController
@RequestMapping("/v1/edi")
@CrossOrigin
public class TaskController {
	
	Logger logger = LoggerFactory.getLogger(this.getClass());

    
	@Autowired
	private Map<String,TaskDownHandleService> handelMap;
	
	@Autowired
	TaskCallBackHandle taskCallBackHandle;
	
	@Autowired
	BzTaskCallBackHandle bzTaskCallBackHandle;

	//method=RF-CTU RF-CTU换箱
	//{"whseid":"仓库号","lpn":"箱号"}
	//method=PC-BZ PC-播种
	//[{"whseid":"仓库号","wavekey":"波次号","rtxtaskgrp":"任务组号","fromloc":"自库位","toStationCode":"播种设备"}]
	//method=RF-AGV 移动功能【RF-AGV移动】
	//{"whseid":"仓库号","lpn":"箱号"}
	//method=MV-TASK 移动功能【库存查询-立库箱移出】【库存查询-无指令入库】
	//{"whseid":"仓库号","tasktype":"WZLKX，WZLCK，WZLHK，WZLBZHK 多个用逗号隔开","fromid":"立库箱号"}
    @ApiOperation(value = "任务下发")
    @PostMapping("/task/down")
    public String taskdown(HttpServletRequest request){
    	RtxWcsResponse res = new RtxWcsResponse();
    	String uid =UUID.randomUUID().toString();
    	try {
    		//MV-TASK 
    		String method = request.getParameter("method");
    		Assert.hasLength(method,"method不能为空");
    		
    		TaskDownHandleService handle =	handelMap.get(method);
    		
    		String body = request.getReader().lines().collect(Collectors.joining(System.lineSeparator()));
    		Assert.hasLength(method,"method不能为空");
    		
    		logger.info("uid-{}-任务下发-method-{}-请求报文-{}",uid,method,body);
    		handle.handleTaskDown(body,method);
    		
    		res.setSuccess("成功");
    	} catch (Exception e) {
    		logger.error("任务下发异常：",e);
    		res.setFailure(e.getMessage());
    	}
    	
    	//上线联调需要去掉
   		res.setSuccess("成功");
    	
    	logger.info("uid-{}-响应报文-{}",uid,JSON.toJSONString(res));
    	return JSON.toJSONString(res);
    }
    
    
    @ApiOperation(value = "任务回传")
    @PostMapping("/task/callback")
    public String taskCallBack(HttpServletRequest request) throws Exception{
    	String uid =UUID.randomUUID().toString();
    	String body = request.getReader().lines().collect(Collectors.joining(System.lineSeparator()));
    	logger.info("uid-{}-任务回传-请求报文-{}",uid,body);
    	String res = taskCallBackHandle.handleTaskCallback(body);
    	logger.info("uid-{}-任务回传-响应报文-{}",uid,res);
    	return res;
    }
    
    
    @ApiOperation(value = "播种任务下发")
    @PostMapping("/bz/down")
    public String bzdown(HttpServletRequest request){
    	RtxWcsResponse res = new RtxWcsResponse();
    	String uid =UUID.randomUUID().toString();
    	try {
    		//MV-TASK 
    		String method = request.getParameter("method");
    		Assert.hasLength(method,"method不能为空");
    		
    		TaskDownHandleService handle =	handelMap.get(method);
    		
    		String body = request.getReader().lines().collect(Collectors.joining(System.lineSeparator()));
    		Assert.hasLength(method,"method不能为空");
    		
    		logger.info("uid-{}-任务下发-method-{}-请求报文-{}",uid,method,body);
    		handle.handleTaskDown(body,method);
    		
    		
    		res.setSuccess("成功");
    	} catch (Exception e) {
    		logger.error("任务下发异常：",e);
    		res.setFailure(e.getMessage());
    	}
    	
    	//上线联调需要去掉
   		res.setSuccess("成功");
    	logger.info("uid-{}-响应报文-{}",uid,JSON.toJSONString(res));
    	return JSON.toJSONString(res);
    }
    
    
    
    @ApiOperation(value = "播种回传")
    @PostMapping("/bz/callback")
    public String taskCallDown(HttpServletRequest request) throws Exception{
    	String uid =UUID.randomUUID().toString();
		String body = request.getReader().lines().collect(Collectors.joining(System.lineSeparator()));
    	logger.info("uid-{}-播种回传-请求报文-{}",uid,body);
    	String res = bzTaskCallBackHandle.handleTaskCallback(body);
    	logger.info("uid-{}-播种回传-响应报文-{}",uid,res);
    	return res;
    }
}
